package com.xzc.java8.C_Collect;

import com.alibaba.fastjson.JSON;
import org.junit.Test;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static java.util.stream.Collectors.partitioningBy;

public class Prime {

    public boolean isPrime(int candidate) {
//        return IntStream.range(2, candidate).noneMatch(i -> candidate % i == 0);
        int candidateRoot = (int) Math.sqrt((double) candidate);
        return IntStream.rangeClosed(2, candidateRoot).noneMatch(i -> candidate % i == 0);
    }

    public Map<Boolean, List<Integer>> partitionPrimes(int n) {
        return IntStream.rangeClosed(2, n).boxed().collect(partitioningBy(candidate -> isPrime(candidate)));
    }

    @Test
    public void test() {
        Prime one = new Prime();
        Map<Boolean, List<Integer>> out0 = one.partitionPrimes(27);
        System.out.println(JSON.toJSONString(out0, true));

        List<Integer> out1 = IntStream.rangeClosed(2, 27).boxed().collect(Collectors.toList());
        System.out.println(JSON.toJSONString(out1, true));
    }
}
